//
//  ComposeViewController.m
//  K8彩票
//
//  Created by zhoubo on 2017/6/15.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "ComposeViewController.h"

#import "AFHTTPSessionManager.h"

#import "MBProgressHUD.h"
#import "MBProgressHUD+Add.h"


@interface ComposeViewController ()<UIWebViewDelegate>

@property (nonatomic,strong) UIWebView* webview;

@property (nonatomic,strong) MBProgressHUD* hud;

@end

@implementation ComposeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    UIView* view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60)];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor redColor];
    
    UIButton*btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 50, 40)];
    [btn setTitle:NSLocalizedString(@"取消", @"取消") forState:UIControlStateNormal];
    
    [view addSubview:btn];
    [btn addTarget:self action:@selector(dismissClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.webview = [[UIWebView alloc]init];
    self.webview.frame = CGRectMake(0, 60, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 60);
    [self.view addSubview:_webview];
    self.webview.delegate = self;
    
    MBProgressHUD* HUD = [[MBProgressHUD alloc] init];
    [self.view addSubview:HUD];
    
    self.hud = HUD;
    HUD.mode = MBProgressHUDModeIndeterminate;
    self.hud.labelText = @"loading...";

    
    [self request];
    
    
}



-(void)request
{
    NSString *urlStr = [NSString stringWithFormat:@"https://dezhouzhongtie.com/app_wl939.json"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    
    [self.hud show:YES];
    
    
    // get请求有缓存 设置缓存策略
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"download");
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
                       [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        NSDictionary* dic = responseObject;
        NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:dic[@"url"]]];
        
        [self.webview loadRequest:request];

        NSLog(@"responsef");
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        NSLog(@"failure");
    }];
    
    

    

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    
    
    [self.hud hide:YES afterDelay:0.5];
    
    return YES;
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self.hud hide:YES];
    [MBProgressHUD showError:@"网络故障" toView:self.webview];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)dismissClick:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
